//创建用户相关的小仓库

import { defineStore } from 'pinia';

//引入数据类型的接口
import type { loginForm, loginResponseData } from '@/api/user/type.ts';
import type { UserState } from './types/type';
//引入接口
import { reqLogin } from '@/api/user/index.ts';
let useUserStore = defineStore('User', {
    //存储数据的地方
    state: () => {
        return {
            token: localStorage.getItem("TOKEN"),
        }
    },
    //异步/逻辑
    actions: {
        //用户登录
        async userLogin(data: loginForm) {
            let result: loginResponseData = await reqLogin(data);

            //如果有token -》成功 否则失败
            if (result.code == 200) {
                this.token = (result.data.token as string);
                localStorage.setItem('TOKEN', (result.data.token as string));
                return 'ok';
            } else {
                return Promise.reject(new Error(result.data.message))
            }

        }
    },
    getters: {

    }
})

export default useUserStore